package com.sxsihe.heartmate.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;

public class SharePreferenceUtil {
    private static SharePreferenceUtil sharePreferenceUtil;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    public static SharePreferenceUtil getInstance(Context context){
        if(sharePreferenceUtil==null){
            sharePreferenceUtil= new SharePreferenceUtil(context);
        }
        return sharePreferenceUtil;
    }

    private SharePreferenceUtil(Context context) {
        sp = context.getSharedPreferences("ecg_config", context.MODE_PRIVATE);
        editor = sp.edit();
    }


    public void set(String key,String val) {
        editor.putString(key, val);
        editor.commit();
    }

    public void clear() {
        editor.clear();
        editor.commit();
    }

    public String get(String key,String defaultVal) {
        return sp.getString(key,defaultVal);
    }

    public int getColor(String key) {
        return getColor(key,Color.parseColor("#000000"));
    }
    public int getColor(String key,int defaultVal) {
        int color = sp.getInt(key, defaultVal);
        if(color==defaultVal){
             setColor("key", color);
        }
        return color;
    }
    public void setColor(String key,int val) {
        editor.putInt(key,val);
        editor.commit();
    }

}
